//
//  MainTabBarController.m
//  openEye
//
//  Created by 徐悟源 on 16/3/30.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "MainTabBarController.h"
#import "WYMainTabBar.h"
#import "HomeTableViewController.h"
#import "CategoryViewController.h"

@interface MainTabBarController ()<WYMainTabBarDelegate>

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    HomeTableViewController *home = [[HomeTableViewController alloc] init];
    [self addChildVC:home withTitle:@"每日精选"];
    
    CategoryViewController *categories = [[CategoryViewController alloc] init];
    [self addChildVC:categories withTitle:@"往期分类"];
    
}

- (void)addChildVC:(UIViewController *)vc withTitle:(NSString *)title
{
    static WYMainTabBar *myTabBar;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        // 自定义tabbar
        myTabBar = [[WYMainTabBar alloc] initWithFrame:self.tabBar.frame];
        
        // 隐藏系统的tabBar
        self.tabBar.hidden = YES;
        
        myTabBar.delegate = self;
        
        [self.view addSubview:myTabBar];
    });
    
    [myTabBar addTabBarBtnWithName:title];
    
    // 用navigationController包装子控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    vc.title = title;
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = WYColor(141, 200, 81);
    attrs[NSFontAttributeName] = Font_ChinaBold(20);
    
    [vc.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
    
    [self addChildViewController:nav];
    
    
}

- (void)mainTabBar:(UIView *)tabBar didSelectBtnFrom:(NSInteger)From To:(NSInteger)To
{
    self.selectedIndex = To;
}

/** 关闭屏幕默认旋转 在指定控制器添加 */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return toInterfaceOrientation == UIDeviceOrientationPortrait;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}
- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

@end
